import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * 静态路由表
 * 不需要权限，所有角色都可以访问
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/import',
    component: Layout,
    children: [
      { name: 'import', path: '', component: () => import('@/views/import/index') }
    ]
  },


  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
]

// 导入动态的路由
import departments from './modules/departments.js'
import employees from './modules/employees.js'
import setting from './modules/setting.js'
import salarys from './modules/salarys.js'
import social from './modules/social.js'
import attendances from './modules/attendances.js'
import approvals from './modules/approvals.js'
import permission from './modules/permission.js'

// 准备一个动态路由表
export const asyncRoutes = [
  departments,
  employees,
  setting,
  salarys,
  social,
  attendances,
  approvals,
  permission
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),

  // 写路由规则表
  routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
